/* 
 * File:   main.cpp
 * Author: christian.mosquera
 *
 * Created on 28 de agosto de 2012, 14:12
 */
#include <stdio.h>

#include <cstdlib>
#include <istream>
#include <iostream>

#include <libpq-fe.h>


using namespace std;

void exit_nicely(PGconn *conn) {
    PQfinish(conn);
    exit(1);
}

/*
 * 
 */
int main(int argc, char** argv) {
    char *pghost,
            *pgport,
            *pgoptions,
            *pgtty,
            *pglogin,
            *pgpwd;
    char *dbName;
    int nFields;
    int i, j;

    FILE *debug, *exportar;

    PGconn *conn;
    PGresult *res;

    /*
     * begin, by setting the parameters for a backend connection if the
     * parameters are null, then the system will try to use reasonable
     * defaults by looking up environment variables or, failing that,
     * using hardwired constants
     */
    pghost = "172.17.4.224"; /* host name of the backend server */
    pgport = "5432"; /* port of the backend server */
    pgoptions = NULL; /* special options to start up the backend
                                 * server */
    pglogin = "postgres";
    pgpwd = "hol4mundo";
    pgtty = NULL; /* debugging tty for the backend server */
    dbName = "managerusers";

    /* make a connection to the database */
    //    conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName, pglogin, pgpwd);
    //    conn = PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, pglogin, pgpwd);
    char *conninfo = "hostaddr=172.17.4.224 port=5432 dbname=managerusers user=postgres password=hol4mundo";
    conn = PQconnectdb(conninfo);

    cout<<PQstatus(conn);
    
    /*
     * check to see that the backend connection was successfully made
     */
    if (PQstatus(conn) == CONNECTION_BAD) {
        fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
        fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }

    debug = fopen("res.txt", "w");


    /* start a transaction block */
    res = PQexec(conn, "BEGIN");
    if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "BEGIN command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }

    res = PQexec(conn, "SELECT * FROM mu_applications");
    
    PQprintOpt opc = {0};
    
    opc.header = 1;
    opc.align = 0;
    opc.fieldSep = "|";
    
//    exportar = fopen("data.txt", "w");
    PQprint(stdout, res, &opc);
//    fclose(exportar);

    /* first, print out the attribute names */
    nFields = PQnfields(res);
    for (i = 0; i < nFields; i++) {
        fprintf(debug, PQfname(res, i));
        printf("%-15s", PQfname(res, i));
    }
    printf("\n\n");
    fprintf(debug, "\n\n");


    /* next, print out the rows */
    for (i = 0; i < PQntuples(res); i++) {
        for (j = 0; j < nFields; j++) {
            fprintf(debug, PQgetvalue(res, i, j));
            printf("%-15s", PQgetvalue(res, i, j));
        }
        printf("\n");
        fprintf(debug, "\n");
    }
    PQclear(res);

    /* commit the transaction */
    res = PQexec(conn, "COMMIT");
    PQclear(res);

    /* close the connection to the database and cleanup */
    PQfinish(conn);

    /*
     * should PQclear PGresult whenever it is no longer needed to avoid
     * memory leaks
     */
    cout << "Hola mundo\n";

    fclose(debug);

    return 0;
}

